<template>
	<div class="bg">
		<div class="login">
			<el-card class="form-card">
				<div slot="header" class="clearfix">
					<span>注册</span>
				</div>
				<el-form label-position="right" ref='form' :model="form" :rules="rules" label-width="80px">
					<el-form-item label="账号" prop="username">
						<el-input type="text" v-model="form.username"></el-input>
					</el-form-item>
					<el-form-item label="密码" prop="password">
						<el-input type="password" v-model="form.password"></el-input>
					</el-form-item>
					<el-form-item label="确认密码" prop="checkPassword">
						<el-input type="password" v-model="form.checkPassword"></el-input>
					</el-form-item>
					<el-form-item label="姓名" prop="fullname">
						<el-input type="text" v-model="form.fullname"></el-input>
					</el-form-item>
					<el-form-item label="性别">
						<el-radio-group v-model="form.sex">
							<el-radio label="男"></el-radio>
							<el-radio label="女"></el-radio>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="手机" prop="tel">
						<el-input type="number" v-model="form.tel"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click='handleLogin' >注册</el-button>
					</el-form-item>
				</el-form>
				<div class="action-box">
					<el-link href="#/login" target="info" >登录账号</el-link>
					<el-link href="#/" target="info">忘记密码？</el-link>
				</div>
			</el-card>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else if (!/^\d{3,15}$/.test(this.form.password)) {
					callback(new Error('长度在 3 到 20位数字'))
				} else {
					if (this.form.checkPassword !== '') {
						this.$refs.form.validateField('checkPassword');
					}
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.form.password) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			
			return {
				form: {
					username: '',
					password: '',
					checkPassword: '',
					fullname: '',
					sex: '男',
					tel: '',
				},

				rules: {
					username: [
						{ required: true, message: '请输入账户名称', trigger: 'blur' },
						{ min: 3, max: 20, message: '长度在 3 到 5 个字符', trigger: 'blur' }
					],
					password: [
						{ validator: validatePass, trigger: 'blur' }
					],
					checkPassword: [
						{ validator: validatePass2, trigger: 'blur' }
					],
					fullname: [
						{ required: true, message: '请输入账户名称', trigger: 'blur' },
						{ min: 3, max: 20, message: '长度在 3 到 5 个字符', trigger: 'blur' }
					],
					tel: [
						{ required: true, message: '请输入密码', trigger: 'blur' },
						{
							pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
							message: '请输入正确的手机号',
							trigger: 'blur'
						}
					]
				}
			}
		},
		methods: {
			handleLogin() {
				this.$router.replace('/login')
			}
		}
	}
</script>

<style lang="less" scoped>
	.bg {
		width: 100vw;
		height: 100vh;
		background: url(~@/assets/img/register/bg.jpg) no-repeat top center;
		background-size: cover;

		.form-card {
			width: 365px;
			position: absolute;
			top: 50%;
			right: 100px;
			transform: translateY(-50%-15px);

			.action-box {
				display: flex;
				align-items: center;
				justify-content: space-between;
			}
		}
	}
</style>
